package com.gang.music.system.controller;

import com.gang.music.common.AjaxResult;
import com.gang.music.core.annotation.Permission;
import com.gang.music.system.entity.Role;
import com.gang.music.system.entity.User;
import com.gang.music.system.service.RoleService;
import com.gang.music.system.service.UserRoleService;
import com.gang.music.system.service.UserService;
import com.smallchill.core.toolbox.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 管理员管理用户的Controller.
 * 
 */
@Controller
@RequestMapping(value = "/system/user")
public class UserAdminController extends BaseController{

	@Autowired
	private UserService userService;
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private UserRoleService userRoleService;

	@Permission({"user:list"})
	@RequestMapping(value="/list", method = RequestMethod.GET)
	public String list(Model model) {
		List<User> users = userService.findAllWithSongList();
		model.addAttribute("userList", users);

		return "admin/user/userList";
	}

	@Permission({"user:update"})
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") Long id, Model model) {
		
		model.addAttribute("user", userService.selectById(id));
		List<Integer> userRoleIds = userService.findRoleIdsByUserId(id);
		List<Role> roleList = roleService.selectList(null);
		for (Role role : roleList) {
			if(userRoleIds.contains(role.getId())) {
				role.setUserHas(true);
			}
		}
		model.addAttribute("roleList", roleList);
		
		return "admin/user/userInput";
	}
	
	@Permission({"user:add"})
	@RequestMapping(value="/add", method=RequestMethod.GET)
	public String addUserInput(Model model) {
		
		List<Role> roleList = roleService.selectList(null);
		model.addAttribute("roleList", roleList);
		
		return "admin/user/userInput";
	}
	
	@Permission({"user:add"})
	@ResponseBody
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public AjaxResult addUser(User user, String roleIds) {
		
		try {
			List<Integer> roleIdList = null;
			if(!Func.isEmpty(roleIds)) {
				if(roleIds.split(",").length > 0) {
					roleIdList = new ArrayList<>();
					for(String roleId : roleIds.split(",")) {
						roleIdList.add(Integer.valueOf(roleId));
					}
				}
			}
			if(Func.isEmpty(user.getId())) {
				userService.registerUser(user);
				userRoleService.insert(user.getId(), roleIdList);
			}
			else {
				userService.updateById(user);
				userRoleService.update(user.getId(), roleIdList);
			}
			return success();
		} catch (Exception e) {
			e.printStackTrace();
			return error("系统出错");
		}
	}
	
	@Permission({"user:lockOrUnLock"})
	@ResponseBody
	@RequestMapping("/lockOrUnLock")
	public AjaxResult lockOrUnLock(User user) {
		
		try {
			userService.updateById(user);
			return success();
		} catch (Exception e) {
			e.printStackTrace();
			return error("系统出错");
		}
	}

	@Permission({"user:delete"})
	@ResponseBody
	@RequestMapping(value = "delete/{id}")
	public AjaxResult delete(@PathVariable("id") Long id) {
		try {
			userService.deleteById(id);
			return success();
		} catch (Exception e) {
			e.printStackTrace();
			return error("系统出错");
		}
	}

	@ModelAttribute
	public void getUser(@RequestParam(value = "id", defaultValue = "-1") Long id, Model model) {
		if (id != -1) {
			model.addAttribute("user", userService.selectById(id));
		}
	}
}
